home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / rtime.zip / RTIME.C < prev    next >
Text File  |  1987-06-17  |  3KB  |  154 lines

  1. /***************************************************************************
  2.  
  3.         Time the execution of a program
  4.  
  5.     All parameters are passed to the child program exactly as if the child
  6.     had been invoked directly from the command line.
  7.  
  8. for executables -
  9.     usage: rtime child program [child program options] [child program arguments]
  10. for batch files -
  11.     usage: rtime -b batchfile [args]
  12. or
  13.     usage: rtime command /c batchfile [args]
  14.  
  15.     Uses Microsoft C vers 3.0 or 4.0
  16.  
  17.     Created    26mar87    jlw
  18.  
  19. ***************************************************************************/
  20.  
  21. #include    <stdio.h>
  22. #include    <time.h>
  23. #include    <process.h>
  24.  
  25. #define    SECS_MIN    60
  26. #define    MINS_HOUR    60
  27. #define    HOURS_DAY    24
  28.  
  29. int        batch = 0;        /* default to not a batch file */
  30.  
  31.     /* global versions of entry args */
  32. int        ac;
  33. char    **av;
  34.  
  35. main(argc, argv)
  36.     int        argc;
  37.     char    **argv;
  38. {
  39. long    stime, etime;
  40.  
  41.     ac = argc;
  42.     av = argv;
  43.  
  44.     if( --ac ) {
  45.         ++av;
  46.     }
  47.     else {
  48.         usage();
  49.         exit(1);
  50.     }
  51.  
  52.     parse();
  53.  
  54.     /* we must have at least the command name */
  55.     if( !ac ) {
  56.         usage();
  57.         exit(1);
  58.     }
  59.  
  60.     fprintf(stderr,"\nTiming program \"%s\" execution.", *av);
  61.     time(&stime);
  62.     fprintf(stderr,"\nStarting time is\n\t%s\n", ctime(&stime));
  63.     if( batch ) {
  64.         /* replace "rtime -b" with "command /c" */
  65.         argv[0] = "command";
  66.         argv[1] = "/c";
  67.         execute(argv[0], argv);
  68.     }
  69.     else {
  70.         execute(*av, av);
  71.     }
  72.     time(&etime);
  73.     fprintf(stderr,"\nEnding time is\n\t%s", ctime(&etime));
  74.     prt_elapsed_time(stime, etime);
  75. }
  76.  
  77. parse()
  78. {
  79.     if( **av == '-' ) {
  80.         switch( tolower(*++*av) ) {
  81.             case 'b':
  82.                 batch = 1;
  83.                 --ac;
  84.                 ++av;
  85.                 break;
  86.             default:
  87.                 usage();
  88.                 --ac;
  89.                 ++av;
  90.                 break;
  91.         }
  92.     }
  93. }
  94.  
  95.  
  96. execute(child, args)
  97.     char    *child;
  98.     char    **args;
  99. {
  100. int        retchild;
  101.  
  102.     retchild = spawnvp(P_WAIT, child, args);
  103.     switch( retchild ) {
  104.         case 0 :
  105.             /* child succeeded */
  106.             break ;
  107.  
  108.         case -1 :
  109.             fprintf(stderr,"RTIME: - child proc \"%s\" not started\n", child);
  110.             break ;
  111.  
  112.         default :
  113.             fprintf(stderr,"\nRTIME: - child proc \"%s\" failed, returned 0x%02x\n" ,
  114.                 child, retchild) ;
  115.             break ;
  116.     }
  117. }
  118.  
  119.  
  120. prt_elapsed_time(stime, etime)
  121.     long    stime, etime;
  122. {
  123. long    ttime;
  124. long    td, th, tm, h, m, s;
  125.  
  126.     ttime = etime - stime;
  127.     fprintf(stderr,"Elapsed time in seconds is\n\t%ld", ttime);
  128.  
  129.     s    = ttime % SECS_MIN;
  130.     tm    = ttime / SECS_MIN;
  131.     m    = tm % MINS_HOUR;
  132.     th    = tm / MINS_HOUR;
  133.     h    = th % HOURS_DAY;
  134.     td    = th / HOURS_DAY;
  135.  
  136.     fprintf(stderr,"\nElapsed time is\n\t%ld day(s), %ld hour(s), %ld min(s), %ld sec(s)\n",
  137.         td, h, m, s);            
  138. }
  139.  
  140.  
  141. usage()
  142. {
  143.         fprintf(stderr,"\nTo time executable programs the usage is:");
  144.         fprintf(stderr,"\n\t\"rtime program [program_opts] [program_args]\"\n");
  145.  
  146.         fprintf(stderr,"\nTo time batch files the usage is:");
  147.         fprintf(stderr,"\n\t\"rtime -b batchfile [batchfile_args]\"\nor");
  148.         fprintf(stderr,"\n\t\"rtime command /c batchfile [batchfile args]\"\n");
  149.         exit(1);
  150. }
  151.  
  152.  
  153. 
  154.